//
//  HMStatusOriginView.m
//  OC新浪微博
//
//  Created by wyman on 16/4/16.
//  Copyright © 2016年 wyman. All rights reserved.
//

#import "HMStatusOriginView.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>
#import "HMStatusPictureView.h"
@interface HMStatusOriginView()
/**
 *  定义属性,懒加载
 */
@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *sourceLabel;
@property (nonatomic, strong) UIImageView *memberView;
@property (nonatomic, strong) UIImageView *verifiedView;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) HMStatusPictureView *pictureView;

@end


@implementation HMStatusOriginView

//懒加载

//#define <#macro#>
//辅助

-(void)setStatus:(HMStatu *)status
{
    _status = status;
    if (status.user.profile_image_url) {
        [self.iconView sd_setImageWithURL:[NSURL URLWithString:status.user.profile_image_url] placeholderImage:[UIImage imageNamed:@"avatar"]];

    }
    self.nameLabel.text = status.user.screen_name;
    self.timeLabel.text = status.created_at;
    self.sourceLabel.text = status.source;
    self.contentLabel.text = status.text;
    
    self.memberView.image = status.user.mbrankImage;
    
    self.verifiedView.image = status.user.verifiedImage;
    
    self.pictureView.pictureURLs = status.storePictureURLs;
    
//    picutreView.snp_updateConstraints { (make) -> Void in
//        let margin = status?.storePicutreURLs == nil ? 0 : HMStatusCellMargin
//        make.top.equalTo(contentLabel.snp_bottom).offset(margin)
    
    [self.pictureView mas_remakeConstraints:^(MASConstraintMaker *make) {
        CGFloat margin = status.storePictureURLs == nil ? 0 : HMStatusCellMargin;
        
        make.top.equalTo(self.contentLabel.mas_bottom).offset(margin);
    }];


}


-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self prepareUI];
        self.backgroundColor = [UIColor whiteColor];
    }

    return self;
}

-(void)prepareUI
{
    [self addSubview:self.iconView];
    [self addSubview:self.nameLabel];
    [self addSubview:self.timeLabel];
    [self addSubview:self.sourceLabel];
    [self addSubview:self.memberView];
    [self addSubview:self.verifiedView];
    [self addSubview:self.contentLabel];
    [self addSubview:self.pictureView];
    
    
    
    
    /**
     *  设置约束
     */
//    const CGFloat width = 35;
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
//        self.iconView.frame = CGRectMake(0, 0, 35, 35);

        make.leading.top.equalTo(self).offset(HMStatusCellMargin);
        make.width.mas_equalTo(35);
        make.height.mas_equalTo(35);
        
        
    }];
    
    //名称
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconView);
        
        make.leading.equalTo(self.iconView.mas_trailing).offset(HMStatusCellMargin);
    }];
    
    //时间
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.iconView);
        make.leading.equalTo(self.nameLabel);
    }];
    
    //来源
    [self.sourceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.timeLabel.mas_trailing);
        make.centerY.equalTo(self.timeLabel);
    }];
    
    //会员等级
    [self.memberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.nameLabel);
        make.leading.equalTo(self.nameLabel.mas_trailing).offset(HMStatusCellMargin);
        make.width.and.height.mas_equalTo(14);
    }];
    
    //认证图标
    [self.verifiedView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.iconView.mas_trailing);
        make.centerY.equalTo(self.iconView.mas_bottom);
        make.width.and.height.mas_equalTo(17);

    }];
    
    //微博内容
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self).offset(HMStatusCellMargin);
        make.top.equalTo(self.iconView.mas_bottom).offset(HMStatusCellMargin);
        make.trailing.equalTo(self).offset(-HMStatusCellMargin);
    }];
    
    //微博
    self.pictureView.frame = CGRectMake(0, 0, 0, 0);
    [self.pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).offset(HMStatusCellMargin);
        make.leading.equalTo(self).offset(HMStatusCellMargin);
        //            make.trailing.equalTo(self).offset(-HMStatusCellMargin)
        
        // TODO: 测试高度先写死
//        make.height.mas_equalTo(arc4random_uniform(100));
        
    }];

    //自定义的底部和contentView重合
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.pictureView).offset(HMStatusCellMargin);
    }];
    
}

/**
 *  懒加载
 *
 *  @return 返回控件
 */
-(UIImageView *)iconView{
    if (_iconView == nil) {
        _iconView = [[UIImageView alloc]init];
        _iconView.image = [UIImage imageNamed:@"avatar"];
        _iconView.layer.cornerRadius = 17.5;
        _iconView.layer.masksToBounds = YES;
        
    }
    return _iconView;
}
-(UIImageView *)memberView{
    if (_memberView == nil) {
        _memberView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"common_icon_membership"]];
    }
    return _memberView;
}
-(UIImageView *)verifiedView{
    if (_verifiedView == nil) {
        _verifiedView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"avatar_vip"]];
    }
    return _verifiedView;
}
//-(UIImageView *)picutreView{
//    if (_picutreView == nil) {
//        _picutreView = [[UIImageView alloc]init];
//    }
//    return _picutreView;
//}

-(UILabel *)nameLabel{
    if (_nameLabel == nil) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.font = [UIFont systemFontOfSize:14];
    }
    return _nameLabel;
}
-(UILabel *)timeLabel{
    if (_timeLabel == nil) {
        _timeLabel = [[UILabel alloc]init];
        _timeLabel.font = [UIFont systemFontOfSize:11];
        _timeLabel.textColor = [UIColor orangeColor];
    }
    return _timeLabel;
}
-(UILabel *)sourceLabel{
    if (_sourceLabel == nil) {
        _sourceLabel = [[UILabel alloc]init];
        _sourceLabel.font = [UIFont systemFontOfSize:11];
        _sourceLabel.textColor = [UIColor lightGrayColor];
    }
    return _sourceLabel;
}
-(UILabel *)contentLabel{
    if (_contentLabel == nil) {
        _contentLabel = [[UILabel alloc]init];
        _contentLabel.font = [UIFont systemFontOfSize:HMStatusContentFontSize];
        _contentLabel.textColor = [UIColor darkGrayColor];
        int result = arc4random_uniform(100)%2;
        _contentLabel.text = result == 0 ? @"就算大雨就算大雨就算大雨就算大雨就算大雨就算大雨就算大雨就算大雨" : @"就算大雨就算大雨就算大雨就算大雨就算大雨就算大雨就算大雨就算大雨就算大雨就算大雨就算大雨就算大雨就算大雨就算大雨就算大雨就算大雨就算大雨就算大雨就算大雨就算大雨就算大雨就算大雨就算大雨就算大雨就算大雨";
        [_contentLabel sizeToFit];
        _contentLabel.numberOfLines = 0;
    }
    return _contentLabel;
}

-(HMStatusPictureView *)pictureView
{
    if (_pictureView == nil) {
        _pictureView = [[HMStatusPictureView alloc]init];
    }
    return _pictureView;

}
@end
